package The15;

import The07.StringTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @Author XiaoKe
 * @Date 2020/4/29 17:09
 * @Description 测试Channel
 */
public class ReadFile {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("ReadFile.java");

        // 创建FileChannel
        FileChannel fisChannel = fis.getChannel();
        // 定义一个ByteBuffer对象，用于重复取水
        ByteBuffer buffer = ByteBuffer.allocate(256);
        // 将FileChannel中的数据放入ByteBuffer
        while (fisChannel.read(buffer) != -1) {
            // 锁定空白区
            buffer.flip();
            // 创建Charset对象
            Charset charset = Charset.forName("UTF-8");
            // 创建解码器
            CharsetDecoder decoder = charset.newDecoder();
            // 将ByteBuffer内容转码
            CharBuffer buffer1 = decoder.decode(buffer);
            System.out.print(buffer1);
            // 将Buffer初始化，为下一次读取做准备
            buffer.clear();
        }
    }
}
